//
//  LiuVisitorView.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/27.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit
import SnapKit

protocol LiuVisitorViewDelegate:NSObjectProtocol {
    func didLoginAction()
}

class LiuVisitorView: UIView {

    //懒加载旋转的图片
    lazy var cycleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy var maskImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    lazy var homeImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    lazy var messageLabel:UILabel = {
        let label = UILabel()
        
        //设置属性
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 13)
        label.tintColor = UIColor.darkGray
        label.text = "待招商label 待招商label 待招商label 待招商label"
        label.numberOfLines = 0
        return label
    }()
    
    //注册按钮
    private lazy var registerButton:UIButton = {
    
        let button = UIButton()
        
        //设置按钮的属性
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //设置图片的拉伸状态
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        
        //给按钮添加点击事件
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        
        //点击按钮不改变背景图片状态
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()
    
    private lazy var loginButton:UIButton = {
        
        let button = UIButton()
        
        //设置按钮的属性
        button.setTitle("登陆", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //设置图片的拉伸状态
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        
        //给按钮添加点击事件
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        
        //点击按钮不改变背景图片状态
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()
    
    //设置代理
    weak var loginDelegate:LiuVisitorViewDelegate?

    
    //创建view要走的方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //初始化UI
    func setupUI() {
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0/255, alpha: 1)
        
        //添加控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //自动布局
        cycleImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        maskImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        homeImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(maskImageView.snp.bottom).offset(20)
            make.width.equalTo(224)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
        
        
    }
    
    //开启旋转动画
    private func openAnimation() {
        
        //创建动画对象
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //选择目标
        animation.toValue = 2 * Double.pi
        
        //动画时长
        animation.duration = 20
        
        //重复次数
        animation.repeatCount = MAXFLOAT
        
        //动画一直到次数完成
        animation.isRemovedOnCompletion = false
        
        //添加到图片上
        cycleImageView.layer.add(animation, forKey: nil)
        
        
        
        
    }
    
    //这个方法来改变显示的图片和文字内容
    func setLoginView(imageName:String?, messageStr:String?) {
        
        if let imagename = imageName,let messagestr = messageStr {
            self.homeImageView.image = UIImage(named: imagename)
            self.messageLabel.text = messagestr
            self.cycleImageView.isHidden = true
            
            
            
        } else {
            
            //开启动画
            openAnimation()
        }
        
    }
    
    @objc private func buttonAction() {
        loginDelegate?.didLoginAction()
    }

}













